package com.itheima.service;


import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.itheima.api.UserApi;
import com.itheima.api.UserInfoApi;
import com.itheima.domain.UserInfo;
import com.itheima.interceptor.UserHolder;
import com.itheima.vo.UserInfoVo;
import com.itheima.template.AipFaceTemplate;
import com.itheima.template.OssTemplate;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@Service
public class UserInfoService {
    @DubboReference
    private UserInfoApi userInfoApi;

    @Autowired
    private OssTemplate ossTemplate;

    @Autowired
    private AipFaceTemplate aipFaceTemplate;

    @Autowired
    private FastFileStorageClient fastFileStorageClient;

    @Autowired
    private FdfsWebServer webServer;


    @DubboReference
    private UserApi userApi;

    //保存用户信息
    public void save(UserInfo userInfo) {
        userInfoApi.save(userInfo);
    }


    //更新用户头像
    public void updateHead(MultipartFile headPhoto, Long id) throws IOException {
        //1、将图片上传到阿里云oss
        String imageUrl = ossTemplate.upload(headPhoto.getOriginalFilename(), headPhoto.getInputStream());
        //2、调用百度云判断是否包含人脸
        boolean detect = aipFaceTemplate.detect(imageUrl);
        //2.1 如果不包含人脸，抛出异常
        if(!detect) {
            throw new RuntimeException("不包含人脸");
        }else{
            //2.2 包含人脸，调用API更新
            UserInfo userInfo = new UserInfo();
            userInfo.setId(id);
            userInfo.setAvatar(imageUrl);
            userInfoApi.update(userInfo);
        }
    }

    //根据id查寻
    @Cacheable(value = "user",key = "#userId")
    public UserInfoVo findById(Long id) {
        UserInfo userInfo = userInfoApi.findById(id);

        UserInfoVo vo = new UserInfoVo();

        //复制copy同名同类型的属性   vo 信息复制给 userInfo
        BeanUtils.copyProperties(userInfo,vo);

        //判断年龄不能为空
        if(userInfo.getAge() != null) {
            vo.setAge(userInfo.getAge().toString());
        }

        return vo;
    }

    //更新
    @CacheEvict(value = "user",key = "#userId")
    public void update(UserInfo userInfo) {
        userInfoApi.update(userInfo);
    }

    //更新用户头像
    public void uphread(MultipartFile headPhoto) throws IOException {
        String filename = headPhoto.getOriginalFilename();
        //根据.切割文件
        filename = filename.substring(filename.lastIndexOf(".") + 1);
        StorePath storePath = fastFileStorageClient.uploadFile(headPhoto.getInputStream(), headPhoto.getSize(), filename, null);
        String videoUrl = webServer.getWebServerUrl() + storePath.getFullPath();


        Long userId = UserHolder.getUserId();
        userInfoApi.uss(userId,videoUrl);
    }

    //更新手机号
    public void updatephone(String phone) {
        Long userId = UserHolder.getUserId();

        userApi.updatePhoneById(userId,phone);
    }

    //统计
    public Map counts() {
        Long userId = UserHolder.getUserId();
        Map map=userInfoApi.tongji(userId);
        return map;
    }

}
